<!doctype  html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
	<title>历届成员</title>
<style type="text/css">
        body{
               background: url("image/6_bg.png");
               background-size: 100% 100%;
               background-repeat: no-repeat;
               min-height:450px;
               width: 100%;
       }
       form{
       	margin: 0 auto;
       	margin-top:50px;
       	width: 700px;
       	position: relative;
       }
       select{
       	width: 150px;
        height: 20px;
       	margin: 0 auto;
       	position: absolute;
       	left: 275px;
       }
       input{
       	   margin-top: 25px;
       	   width: 200px;

       }
       span{
       	   display: inline-block;
       	   width: 75px;
       	   margin-left: 50px;
       }
       .center{
           margin-top: 20px;
       	   margin-left:220px;
       }
       #file{
          display: none;
       }
       #upload{
          width:120px;
       }
       #smallimg{
          height: 35px;
          width: 35px;
          position: relative;
          top:15px;
          left:30px;

       }
       .leftbutton{
       	   margin-left: 250px;
       }
       #button{
          width: 100px;
          margin-left: 20px;
       }
</style>
</head>
<body>
	<form>
            <select>
		           <option>历届成员</option>
		 	</select><br>
		     <span>姓名：</span><input type="text" name="name" id="name">
		     <span>部门：</span><input type="text" name="department" id="department"><br>
		     <span>职务：</span><input type="text" name="position" id="position">
		     <span>籍贯：</span><input type="text" name="from" id="from"><br>
		     <span>生日：</span><input type="text" name="birthday" id="birthday">
		     <span>专业：</span><input type="text" name="major" id="major"><br>
		     <span>Q Q：</span><input type="text" name="qq" id="qq">
		     <span>Email：</span><input type="email" name="email" id="email"><br>
		     <span>爱好：</span><input type="text" name="bobby" id="hobby">
		     <span>简介：</span><input type="text" name="intro" id="intro"><br>
		     <span class="center">照片：</span><input type="file" name="file" id="file"><input type="button" id="upload" value="上传"><img src="image/6_bg.png" id="smallimg"><br>
         <a class="leftbutton" href="content_mag.html">返回</a>
		     <input type="button" id="button" value="提交" >
	</form>
  <script type="text/javascript" src="js/jquery.min.js"></script>
  <script type="text/javascript" src="../public/js/serverIp.js"></script>
  <script type="text/javascript">

     $(function(){


        var array=window.location.href.split("?");
        var substr=array[1].split("&");
        alert(window.location.href)
        var datastr="type=ID&bool=false&columnId="+substr[1]+"&modelId="+substr[0]+"&id="+substr[3];
        if(substr[2]=="edit"){
            $.post(serverIp+"/Scauaie.com/artical/showAll",datastr,function(data){
              
                 $("#name").val(data[0].name);
                 $("#department").val(data[0].department);
                 $("#position").val(data[0].position);
                 $("#from").val(data[0].birthplace);
                 $("#birthday").val(data[0].birthday);
                 $("#major").val(data[0].profession);
                 $("#qq").val(data[0].qq);
                 $("#email").val(data[0].email);
                 $("#hobby").val(data[0].specialty);
                 $("#intro").val(data[0].profile);
                 $("smallimg").attr({"src":data[0].photoUrl});
            },"json")

        }


         $("#upload").click(function(){

                $("#file").trigger("click");
                $("#file").change(function(){
                       // alert($("#file")[0].files)   获取文件列表
                       var files=$(this).prop("files");
                       var formdata = new FormData();
                       formdata.append("file",files[0]);
                       var filename=$(this).val();
                       var pictest = /\.jpg$|\.jpeg$|\.gif$|\.png$/i;

                       if(!pictest.test(filename)){
                          alert("文件格式不支持！");
                       }else{
                            $.ajax({
                                type: "post",
                                url: serverIp+"/Scauaie.com/artical/file/upload",
                                data:formdata,
                                dataType:"json",
                                cache: false,  
                                processData: false,  
                                contentType: false,
                                success:function(data){
                                   $("#smallimg").attr({"src":data.result});
                              }
                           })
                       }   
                 })
         })


        function submitTo(url,id){

             var arr=window.location.href.split("?");
              var sub=arr[1].split("&");
              var time="2016/4/14"; 
              var name=$("#name").val();
              var department=$("#department").val();
              var position=$("#position").val();
              var birthplace=$("#from").val();
              var birthday=$("#birthday").val();
              var profession=$("#major").val();
              var qq=$("#qq").val();
              var email=$("#email").val();
              var specialty=$("#hobby").val();
              var profile=$("#intro").val();
              var photoUrl=$("#smallimg").attr("src");
                
              var data={
                  "type":"COLUMN",
                  "bool":false,
                  "modelId":sub[0],
                  "columnId":sub[1],
                  "name":name,
                  "department":department,
                  "position":position,
                  "birthplace":birthplace,
                  "birthday":birthday,
                  "profession":profession,
                  "qq":qq,
                  "email":email,
                  "specialty":specialty,
                  "profile":profile,
                  "photoUrl":photoUrl,
                  "id":id,
                  "people_id":id

              };

               if(!name||!department||!position||!birthplace||!birthday||!profession||!qq||!email||!specialty||!profile||!photoUrl){
                      alert("请先填写完整");
               }else{
                  $.post(url,{"data":JSON.stringify(data)},function(data){
                        var dataobj=JSON.parse(data);
                        if(dataobj.bool=="true"){
                            if(confirm("提交成功，留在本页面？")){
                               var href=window.location.href;
                               window.location.href=href;
                            }else{
                               window.location.href="content_mag.html";
                            }
                        }else{
                           alert("提交失败，请重新尝试.")                          
                        } 
                   },"html")
               }
       }

       $("#button").click(function(){          
             if(substr[2]=="edit"){
              alert(1)
                  submitTo(serverIp+"/Scauaie.com/artical/edit",substr[3]);

             }else{
              alert(2)
                  submitTo(serverIp+"/Scauaie.com/artical/add",null);
             }  
        })

     })

  </script>
</body>
</html>	